package com.photoeditor.demo.ui.widget.image.edit;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.RelativeLayout;

import com.photoeditor.R;
import com.photoeditor.demo.app.ItemClickSupport;
import com.photoeditor.demo.statistics.StatisticsConstant;
import com.photoeditor.demo.statistics.StatisticsUtils;
import com.photoeditor.demo.ui.activity.edit.EditorImageActivity;
import com.photoeditor.demo.ui.widget.HRecyclerView;
import com.photoeditor.demo.ui.widget.image.mixer.ImageMixerView;
import com.photoeditor.demo.ui.widget.image.mixer.MixerMode;
import com.photoeditor.demo.ui.widget.image.mixer.MixerModeAdapter;


public class MixerBarView extends RelativeLayout implements IDealInit, IEditOpearation {

    private ImageMixerView mMixerView;

    private EditorImageActivity mActivity;

    private CustomTabButton mReplaceImageButton;
    private CustomTabButton mEraseButton;
    private CustomTabButton mMixerButton;

    private ViewGroup mMixModeLayout;
    private HRecyclerView mMixModeListView;
    private MixerMode mCurMixMode;
    private MixerMode mSelMixMode;

    private boolean mUsedBlend = false;

    public MixerBarView(Context context) {
        super(context);
    }

    public MixerBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MixerBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    @Override
    public void init() {
        initViews();
    }

    /**
     * 初始化
     */
    private void initViews() {
        hookupGraffitoButtons();
        initView(getResources().getColor(R.color.image_edit_sencond_text_color),
                getResources().getColor(R.color.accent_color));
        mCurMixMode = new MixerMode(MixerMode.MODE_NORMAL);
        mSelMixMode = mCurMixMode;
    }

    private OnClickListener mClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.btn_mixer_replace) {
                mActivity.replaceBitmapForMixer();
            } else if (id == R.id.btn_mixer_erase) {
                mActivity.openEraseForMixer(true, false);
            } else if (id == R.id.btn_mixer_mix) {
                if (mMixModeLayout == null) {
                    initMixModeLayout();
                }
                updateMixModeAdapter(mCurMixMode);
                mMixModeListView.getAdapter().notifyDataSetChanged();
                mMixModeLayout.setVisibility(VISIBLE);
                mActivity.showInsideBottomBarWithName(R.string.mixer_blend, View.NO_ID);
                StatisticsUtils.statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_EDITOR,
                        StatisticsConstant.UMENG_CUSTOM_KEY_FUNCTION, StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_PREVIEW_VALUE_EDIT_BLEND_CLICK);
            }

        }
    };

    private void hookupGraffitoButtons() {

        mReplaceImageButton = (CustomTabButton) findViewById(R.id.btn_mixer_replace);
        mReplaceImageButton.setOnClickListener(mClickListener);

        mEraseButton = (CustomTabButton) findViewById(R.id.btn_mixer_erase);
        mEraseButton.setOnClickListener(mClickListener);

        mMixerButton = (CustomTabButton) findViewById(R.id.btn_mixer_mix);
        mMixerButton.setOnClickListener(mClickListener);

    }

    public void initView(int primaryColor, int emphasisColor) {

        mReplaceImageButton.setThemeImageRes(R.drawable.icon_grid_replace,
                View.NO_ID);
        mReplaceImageButton.setTextColor(primaryColor, primaryColor);
        mEraseButton.setThemeImageRes(R.drawable.ic_edit_eraser,
                View.NO_ID);
        mEraseButton.setTextColor(primaryColor, primaryColor);

        mMixerButton.setThemeImageRes(R.drawable.ic_tool_blend, View.NO_ID);
        mMixerButton.setTextColor(primaryColor, primaryColor);

    }

    public void setMixerView(ImageMixerView mixerView) {
        mMixerView = mixerView;
        mMixerView.setOnImageDeleteListener(new ImageMixerView.OnImageDeleteListener() {
            @Override
            public void onDelete() {
                setEraserAndMixerBtnEnable(false);
                mCurMixMode = new MixerMode(MixerMode.MODE_NORMAL);
                mSelMixMode = mCurMixMode;
                updateMixModeAdapter(mCurMixMode);
            }
        });
    }

    public void setEraserAndMixerBtnEnable(boolean enable) {
        mEraseButton.setEnabled(enable);
        mMixerButton.setEnabled(enable);
        float alpha = enable ? 1 : 0.5f;
        mEraseButton.setAlpha(alpha);
        mMixerButton.setAlpha(alpha);
    }

    public void setActivity(EditorImageActivity activity) {
        mActivity = activity;
    }

    public void reset() {
        mCurMixMode = new MixerMode(MixerMode.MODE_NORMAL);
        mSelMixMode = mCurMixMode;
        if (mMixModeLayout != null) {
            mMixModeLayout.setVisibility(GONE);
        }
        setEraserAndMixerBtnEnable(true);
    }

    private void initMixModeLayout() {
        if (mMixModeLayout == null) {
            ViewStub viewStub = findViewById(R.id.mixer_mode_stub);
            mMixModeLayout = (ViewGroup) viewStub.inflate();
            mMixModeListView = mMixModeLayout.findViewById(R.id.mixer_mode_listview);
            mCurMixMode = new MixerMode(MixerMode.MODE_NORMAL);
            mSelMixMode = mCurMixMode;
            MixerModeAdapter adapter = new MixerModeAdapter();
            adapter.setData(MixerMode.loadAllMixerMode());
            mMixModeListView.setAdapter(adapter);
            ItemClickSupport itemClickSupport = new ItemClickSupport(mMixModeListView, adapter);
            adapter.setItemClickSupport(itemClickSupport);
            itemClickSupport.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView recyclerView, View view, int position, long id) {
                    MixerModeAdapter adapter = (MixerModeAdapter) recyclerView.getAdapter();
                    if (adapter.getData() != null && adapter.getData().size() > position) {
                        MixerMode mode = adapter.getData().get(position);
                        if (mMixerView != null) {
                            mCurMixMode = mode;
                            mMixerView.setMixMode(mode.getFilter());
                            adapter.setSeletcItemPosition(position, view);
                            mUsedBlend = true;
                        }
                    }
                }
            });
        }
    }

    private void updateMixModeAdapter(MixerMode mode) {
        if (mMixModeListView == null) {
            return;
        }
        if (mode != null) {
            MixerModeAdapter adapter = (MixerModeAdapter) mMixModeListView.getAdapter();
            int position = adapter.getPositionByMode(mode);
            ((MixerModeAdapter) mMixModeListView.getAdapter()).setSeletcItemPosition(position);
            mMixModeListView.setSelectionNoAni(position);
        } else {
            ((MixerModeAdapter) mMixModeListView.getAdapter()).setSeletcItemPosition(-1);
            mMixModeListView.setSelectionNoAni(-1);
        }
    }

    @Override
    public boolean onConfirmClick() {
        if (mMixModeLayout != null && mMixModeLayout.getVisibility() == VISIBLE) {
            mMixModeLayout.setVisibility(GONE);
            mActivity.showInsideBottomBarWithName(R.string.image_edit_mixer, R.drawable.ic_tool_help);
            mSelMixMode = mCurMixMode;
            updateMixModeAdapter(mSelMixMode);
            mMixerView.setMixMode(mSelMixMode.getFilter());

            if (mUsedBlend) {
                StatisticsUtils.statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_EDITOR,
                        StatisticsConstant.UMENG_CUSTOM_KEY_FUNCTION, StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_PREVIEW_VALUE_EDIT_BLEND_SAVE);
            }
            return false;
        }
        return true;
    }

    @Override
    public boolean onCancelClick() {
        if (mMixModeLayout != null && mMixModeLayout.getVisibility() == VISIBLE) {
            mMixModeLayout.setVisibility(GONE);
            mActivity.showInsideBottomBarWithName(R.string.image_edit_mixer, R.drawable.ic_tool_help);
            mCurMixMode = mSelMixMode;
            updateMixModeAdapter(mSelMixMode);
            mMixerView.setMixMode(mSelMixMode.getFilter());
            return false;
        }
        return true;
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == View.VISIBLE) {
            StatisticsUtils.statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_EDITOR,
                    StatisticsConstant.UMENG_CUSTOM_KEY_FUNCTION, StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_PREVIEW_VALUE_EDIT_BLEND_SHOW);
        }
    }

}
